//
//  HomeDataHandle.m
//  MarryMe
//
//  Created by lanou3g on 16/1/5.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "HomeDataHandle.h"
#import "AFNetworking.h"
#import "Homemodel.h"
static HomeDataHandle *_homeHandle =nil;
@implementation HomeDataHandle
+(instancetype)shareHomeDataHandle{
    return [[self alloc]init];
    
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _homeHandle = [super allocWithZone:zone];
        
    });
    return _homeHandle;
    
}
-(void)getDataFromNet1:(ReloadDataBlock)finisheBlock{
    //创建管理类
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    //设置请求格式
    sessionManger.requestSerializer = [AFHTTPRequestSerializer serializer];
    //设置返回格式
    sessionManger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [sessionManger GET:@"http://www.hunliji.com/p/wedding/index.php/home/APICity/cities?relative_id=1" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {

        NSString *str  =  [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *resData = [[NSData alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        
        //系统自带JSON解析
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        
        NSDictionary  *dataDic = [resultDic valueForKey:@"data"];
              NSDictionary * listDic = [dataDic valueForKey:@"list"];
  NSArray  *arr1= [NSArray arrayWithArray:[listDic allKeys]];
    self.keyArray  =   [arr1 sortedArrayUsingSelector:@selector(compare:)];
        NSMutableArray * listArray = [NSMutableArray array];
        for (NSString *key in self.keyArray) {
            NSArray * arr = [listDic valueForKey:key];
            NSMutableArray *arr1 = [NSMutableArray array];
                   for (NSDictionary *dic in arr) {
                 Homemodel * model =[[ Homemodel alloc]init];
                       [model setValuesForKeysWithDictionary:dic];
                          [arr1 addObject:model];
                     }
                    [listArray addObject:arr1];
                 }
        self.listDataArray = [NSArray arrayWithArray:listArray];
        NSArray *array1 =  dataDic[@"hot_city"];
        NSMutableArray * hotArr = [NSMutableArray array];
        for (NSDictionary *dic in array1) {
            Homemodel *model = [[Homemodel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [hotArr addObject:model];
              }
        self.hotDataArray = [NSArray arrayWithArray:hotArr];
        NSLog(@"请求成功");
        dispatch_async(dispatch_get_main_queue(), ^{
            
            finisheBlock(YES);

        });

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败");
    }];

    


}



@end
